package com.wufun.overseas.tools.filetransfer;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

/**
 * Wifi 工具类
 * <p>
 * 封装了Wifi的基础操作方法，方便获取Wifi连接信息以及操作Wifi
 */

public class WifiUtils {
    private static WifiUtils mWifiUtils = null;
    private List<WifiConfiguration> mWifiConfiguration;
    private WifiInfo mWifiInfo;
    private DhcpInfo mDhcpInfo;
    private List<ScanResult> mWifiList;
    private WifiManager.WifiLock mWifiLock;
    public WifiManager mWifiManager;
    private NetworkInfo mNetworkInfo;
    private static Context mContext;
    private DhcpInfo dhcpInfo;

    private WifiUtils(Context paramContext) {
        mWifiManager = (WifiManager) paramContext.getSystemService(Context.WIFI_SERVICE);
        mDhcpInfo = mWifiManager.getDhcpInfo();
        mWifiInfo = mWifiManager.getConnectionInfo();
        mNetworkInfo = ((ConnectivityManager) paramContext
                .getSystemService(Context.CONNECTIVITY_SERVICE))
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mContext = paramContext;
    }


    public void setNewWifiManagerInfo() {
        mWifiInfo = mWifiManager.getConnectionInfo();
        mDhcpInfo = mWifiManager.getDhcpInfo();
    }

    public static WifiUtils getInstance(Context paramContext) {
        if (mWifiUtils == null)
            mWifiUtils = new WifiUtils(paramContext);
        return mWifiUtils;
    }

    private WifiConfiguration isExsits(String paramString) {
        if (mWifiManager == null || mWifiManager.getConfiguredNetworks() == null) return null;
        Iterator<WifiConfiguration> localIterator = mWifiManager.getConfiguredNetworks().iterator();
        WifiConfiguration localWifiConfiguration;
        do {
            if (!localIterator.hasNext())
                return null;
            localWifiConfiguration = localIterator.next();
            Log.d("Ap", "isExsits: " + localWifiConfiguration.SSID);
        }
        while (!localWifiConfiguration.SSID.equals("\"" + paramString + "\""));
        return localWifiConfiguration;
    }

    /**
     * 获取热点状态
     *
     * @return boolean值，对应热点的开启(true)和关闭(false)
     */
    public boolean getWifiApState() {
        try {
            int i = ((Integer) mWifiManager.getClass().getMethod("getWifiApState", new Class[0])
                    .invoke(mWifiManager)).intValue();
            return (3 == i) || (13 == i);
        } catch (Exception localException) {
        }
        return false;
    }

    /**
     * 判断是否连接上wifi
     *
     * @return boolean值(isConnect), 对应已连接(true)和未连接(false)
     */
    public boolean isWifiConnect() {
        return mNetworkInfo.isConnected();
    }

    public boolean isWifiConnect(Context context) {
        mNetworkInfo = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE))
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean f = mNetworkInfo.isConnected();
        if (f) return f;
        f = mNetworkInfo.isAvailable();
        return f;
    }

    public void AcquireWifiLock() {
        mWifiLock.acquire();
    }

    public void CreatWifiLock() {
        mWifiLock = mWifiManager.createWifiLock("Test");
    }

    //打开wifi
    public void OpenWifi() {
        try {
            mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            if (!mWifiManager.isWifiEnabled()) {
                mWifiManager.setWifiEnabled(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openWifi(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!isWifiEnable()) {
            mWifiManager.setWifiEnabled(true);
        }
    }

    public boolean isWifiEnable() {
        boolean rs = mWifiManager.isWifiEnabled();
        return rs;
    }

    public void ReleaseWifiLock() {
        if (mWifiLock.isHeld())
            mWifiLock.release();
    }

    public  boolean isConnectToHotSpotRunning=false;
    /**
     * Method to Get Network Security Mode
     *
     * @param scanResult
     * @return OPEN PSK EAP OR WEP
     */
    public String getSecurityMode(ScanResult scanResult) {
        final String cap = scanResult.capabilities;
        final String[] modes = {"WPA", "EAP","WEP" };
        for (int i = modes.length - 1; i >= 0; i--) {
            if (cap.contains(modes[i])) {
                return modes[i];
            }
        }
        return "OPEN";
    }

    /**
     *shred  Configured wifi Network By SSID
     * @param ssid of wifi Network
     */
    public void removeWifiNetwork(String ssid) {
        List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
        if (configs != null) {
            for (WifiConfiguration config : configs) {
                if (config.SSID.contains(ssid)) {
                    mWifiManager.disableNetwork(config.networkId);
                    mWifiManager.removeNetwork(config.networkId);
                }
            }
        }
        mWifiManager.saveConfiguration();
    }


    //关闭wifi
    public void closeWifi() {
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
        }
    }

    public void closeWifi(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiManager.setWifiEnabled(false);
    }

    public void connectConfiguration(int paramInt) {
        if (paramInt > mWifiConfiguration.size())
            return;
        mWifiManager.enableNetwork(
                mWifiConfiguration.get(paramInt).networkId, true);
    }

    public void disableNetwork(Context context, int netId) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiManager.disableNetwork(netId);
        mWifiManager.saveConfiguration();
    }

    public void removeNetwork(Context context, int netId) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiManager.removeNetwork(netId);
        mWifiManager.saveConfiguration();

    }

    /**
     * 根据wifi信息创建或关闭一个热点
     *
     * @param paramWifiConfiguration
     * @param paramBoolean           关闭标志
     */
    public void createWiFiAP(WifiConfiguration paramWifiConfiguration, boolean paramBoolean) {
        try {
            Class<? extends WifiManager> localClass = mWifiManager.getClass();
            Method setupMethod = localClass.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            WifiManager localWifiManager = mWifiManager;
            setupMethod.invoke(localWifiManager, paramWifiConfiguration, paramBoolean);
            return;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }



    public WifiConfiguration createAPInfo(String ssid, String password) {
        WifiConfiguration netConfig = new WifiConfiguration();
        // 设置wifi热点名称
        netConfig.SSID = ssid;

        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);


        if (password != null) {
            // 设置wifi热点密码
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//            netConfig.allowedKeyManagement.set(4);
            netConfig.preSharedKey = password;
        }
        return netConfig;
    }

    /**
     * 创建一个wifi信息
     *
     * @param ssid         名称
     * @param paramString2 密码
     * @param paramInt     有3个参数，1是无密码，2是简单密码，3是wap加密
     * @param paramString3 是"ap热点"还是"wifi"
     * @return
     */
    public WifiConfiguration createWifiInfo(String ssid, String paramString2, int paramInt,
                                            String paramString3) {
        WifiConfiguration localWifiConfiguration1 = new WifiConfiguration();
        localWifiConfiguration1.allowedAuthAlgorithms.clear();
        localWifiConfiguration1.allowedGroupCiphers.clear();
        localWifiConfiguration1.allowedKeyManagement.clear();
        localWifiConfiguration1.allowedPairwiseCiphers.clear();
        localWifiConfiguration1.allowedProtocols.clear();
        if ("wt".equals(paramString3)) {//wifi链接
            localWifiConfiguration1.SSID = ("\"" + ssid + "\"");
            WifiConfiguration localWifiConfiguration2 = isExsits(ssid);
            if (localWifiConfiguration2 != null)
                removeNetwork(mContext, localWifiConfiguration2.networkId);//删除指定的网络
            if (paramInt == 1) {
                localWifiConfiguration1.wepKeys[0] = "";
                localWifiConfiguration1.allowedKeyManagement.set(0);
                localWifiConfiguration1.wepTxKeyIndex = 0;
            } else if (paramInt == 2) {
                localWifiConfiguration1.hiddenSSID = false;
                localWifiConfiguration1.wepKeys[0] = ("\"" + paramString2 + "\"");
            } else {
                localWifiConfiguration1.preSharedKey = ("\"" + paramString2 + "\"");
                localWifiConfiguration1.hiddenSSID = false;
               /* localWifiConfiguration1.allowedAuthAlgorithms.set(0);
                localWifiConfiguration1.allowedGroupCiphers.set(2);
                localWifiConfiguration1.allowedKeyManagement.set(1);
                localWifiConfiguration1.allowedPairwiseCiphers.set(1);
                localWifiConfiguration1.allowedGroupCiphers.set(3);
                localWifiConfiguration1.allowedPairwiseCiphers.set(2);*/

                localWifiConfiguration1.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                localWifiConfiguration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                localWifiConfiguration1.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                localWifiConfiguration1.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                localWifiConfiguration1.status = WifiConfiguration.Status.ENABLED;
            }
        } else {//wifi 热点
            localWifiConfiguration1.SSID = ssid;
            localWifiConfiguration1.allowedAuthAlgorithms.set(1);
            localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            localWifiConfiguration1.allowedKeyManagement.set(0);
            localWifiConfiguration1.wepTxKeyIndex = 0;
            if (paramInt == 1) {//无密码
                localWifiConfiguration1.wepKeys[0] = "";
                localWifiConfiguration1.allowedKeyManagement.set(0);
                localWifiConfiguration1.wepTxKeyIndex = 0;
            } else if (paramInt == 2) {//简单密码
                localWifiConfiguration1.hiddenSSID = false;//网络上是否广播ssid
                localWifiConfiguration1.wepKeys[0] = paramString2;
            } else if (paramInt == 3) {//wap加密
                localWifiConfiguration1.preSharedKey = paramString2;
                localWifiConfiguration1.allowedAuthAlgorithms.set(0);
                localWifiConfiguration1.allowedProtocols.set(1);
                localWifiConfiguration1.allowedProtocols.set(0);
                localWifiConfiguration1.allowedKeyManagement.set(1);
                localWifiConfiguration1.allowedPairwiseCiphers.set(2);
                localWifiConfiguration1.allowedPairwiseCiphers.set(1);
            }
        }
        return localWifiConfiguration1;
    }

    public void disconnectWifi(int paramInt) {
        mWifiManager.disableNetwork(paramInt);
    }

    //获得热点名
    public String getApSSID() {
        try {
            Method localMethod = mWifiManager.getClass().getDeclaredMethod(
                    "getWifiApConfiguration");
            if (localMethod == null)
                return null;
            Object localObject1 = localMethod.invoke(mWifiManager);
            if (localObject1 == null)
                return null;
            WifiConfiguration localWifiConfiguration = (WifiConfiguration) localObject1;
            if (localWifiConfiguration.SSID != null)
                return localWifiConfiguration.SSID;
            Field localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
            if (localField1 == null)
                return null;
            localField1.setAccessible(true);
            Object localObject2 = localField1.get(localWifiConfiguration);
            localField1.setAccessible(false);
            if (localObject2 == null)
                return null;
            Field localField2 = localObject2.getClass().getDeclaredField("SSID");
            localField2.setAccessible(true);
            Object localObject3 = localField2.get(localObject2);
            if (localObject3 == null)
                return null;
            localField2.setAccessible(false);
            String str = (String) localObject3;
            return str;
        } catch (Exception localException) {
        }
        return null;
    }


    public WifiInfo getmWifiInfo() {
        return mWifiInfo;
    }

    //获得wifi 名
    public String getBSSID() {
        if (mWifiInfo == null)
            return "NULL";
        return mWifiInfo.getBSSID();
    }

    public String getSSID() {
        if (mWifiInfo == null)
            return "NULL";
        String ssid = mWifiInfo.getSSID();
        if (ssid == null || ssid.trim().equals("")) {
            return "";
        }
        ssid = ssid.replaceAll("\"", "");
        return ssid;
    }

    public String getSSID(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiInfo = mWifiManager.getConnectionInfo();
        String ssid = mWifiInfo.getSSID();
        if (ssid == null) return "";
        ssid = ssid.replaceAll("\"", "");
        return ssid;
    }

    public List<WifiConfiguration> getConfiguration() {
        return mWifiConfiguration;
    }

    //获得IP地址
    public String getLocalIPAddress() {
        if (mWifiInfo == null)
            return "NULL";
        return intToIp(mWifiInfo.getIpAddress());
    }

    public String getServerIPAddress() {
        if (mDhcpInfo == null)
            return "NULL";
        return intToIp(mDhcpInfo.serverAddress);
    }

    public String getMacAddress() {
        if (mWifiInfo == null)
            return "NULL";
        return mWifiInfo.getMacAddress();
    }

    public int getNetworkId() {
        if (mWifiInfo == null)
            return 0;
        return mWifiInfo.getNetworkId();
    }

    public int getWifiApStateInt() {
        try {
            int i = ((Integer) mWifiManager.getClass().getMethod("getWifiApState", new Class[0]).invoke(mWifiManager)).intValue();
            return i;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return 4;
    }

    public WifiInfo getWifiInfo() {
        return mWifiManager.getConnectionInfo();
    }

    public List<ScanResult> getWifiList() {
        return mWifiList;
    }

    public StringBuilder lookUpScan() {
        StringBuilder localStringBuilder = new StringBuilder();
        for (int i = 0; ; i++) {
            if (i >= 2)
                return localStringBuilder;
            localStringBuilder.append("Index_" + Integer.valueOf(i + 1).toString() + ":");
            localStringBuilder.append(mWifiList.get(i).toString());
            localStringBuilder.append("/n");
        }
    }

    public void setWifiList() {
        mWifiList = mWifiManager.getScanResults();
    }

    public void startScan(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiManager.startScan();
        mWifiConfiguration = mWifiManager.getConfiguredNetworks();
    }

    public String intToIp(int paramIntip) {
        return (paramIntip & 0xFF) + "." + ((paramIntip >> 8) & 0xFF) + "."
                + ((paramIntip >> 16) & 0xFF) + "." + ((paramIntip >> 24) & 0xFF);
    }


    public boolean connectToHotspot(String netSSID, String netPass) {

        isConnectToHotSpotRunning= true;
        WifiConfiguration wifiConf = new WifiConfiguration();
        List<ScanResult> scanResultList=mWifiManager.getScanResults();

        if(mWifiManager.isWifiEnabled()){

            for (ScanResult result : scanResultList) {

                if (result.SSID.equals(netSSID)) {

                    removeWifiNetwork(result.SSID);
                    String mode = getSecurityMode(result);

                    if (mode.equalsIgnoreCase("OPEN")) {

                        wifiConf.SSID = "\"" + netSSID + "\"";
                        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        int res = mWifiManager.addNetwork(wifiConf);
                        mWifiManager.disconnect();
                        mWifiManager.enableNetwork(res, true);
                        mWifiManager.reconnect();
                        mWifiManager.setWifiEnabled(true);
                        isConnectToHotSpotRunning=false;
                        return true;

                    } else if (mode.equalsIgnoreCase("WEP")) {

                        wifiConf.SSID = "\"" + netSSID + "\"";
                        wifiConf.wepKeys[0] = "\"" + netPass + "\"";
                        wifiConf.wepTxKeyIndex = 0;
                        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        int res = mWifiManager.addNetwork(wifiConf);
                        mWifiManager.disconnect();
                        mWifiManager.enableNetwork(res, true);
                        mWifiManager.reconnect();
                        mWifiManager.setWifiEnabled(true);
                        isConnectToHotSpotRunning=false;
                        return true;

                    }else{

                        wifiConf.SSID = "\"" + netSSID + "\"";
                        wifiConf.preSharedKey = "\"" + netPass + "\"";
                        wifiConf.hiddenSSID = false;
                        wifiConf.status = WifiConfiguration.Status.ENABLED;
                        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                        wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        int res = mWifiManager.addNetwork(wifiConf);
                        mWifiManager.disconnect();

                        if(connectWifiByReflectMethod(res)==null){
                            mWifiManager.enableNetwork(res, true);
                        }
                        mWifiManager.saveConfiguration();
                        mWifiManager.reconnect();
//
//                        mWifiManager.setWifiEnabled(true);
                        isConnectToHotSpotRunning=false;
                        return true;

                    }
                }
            }
        }
        isConnectToHotSpotRunning=false;
        return false;
    }

    public void addNetwork(Context context, WifiConfiguration paramWifiConfiguration) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int i = mWifiManager.addNetwork(paramWifiConfiguration);

        mWifiManager.enableNetwork(i, true);
    }
    /**
     * 通过反射出不同版本的connect方法来连接Wifi
     *
     * @author jiangping.li
     * @param netId
     * @return
     * @since MT 1.0
     *
     */
    private Method connectWifiByReflectMethod(int netId) {
        Method connectMethod = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            // 反射方法： connect(int, listener) , 4.2 <= phone's android version
            for (Method methodSub : mWifiManager.getClass()
                    .getDeclaredMethods()) {
                if ("connect".equalsIgnoreCase(methodSub.getName())) {
                    Class<?>[] types = methodSub.getParameterTypes();
                    if (types != null && types.length > 0) {
                        if ("int".equalsIgnoreCase(types[0].getName())) {
                            connectMethod = methodSub;
                        }
                    }
                }
            }
            if (connectMethod != null) {
                try {
                    connectMethod.invoke(mWifiManager, netId, null);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) {
            // 反射方法: connect(Channel c, int networkId, ActionListener listener)
            // 暂时不处理4.1的情况 , 4.1 == phone's android version
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            // 反射方法：connectNetwork(int networkId) ,
            // 4.0 <= phone's android version < 4.1
            for (Method methodSub : mWifiManager.getClass()
                    .getDeclaredMethods()) {
                if ("connectNetwork".equalsIgnoreCase(methodSub.getName())) {
                    Class<?>[] types = methodSub.getParameterTypes();
                    if (types != null && types.length > 0) {
                        if ("int".equalsIgnoreCase(types[0].getName())) {
                            connectMethod = methodSub;
                        }
                    }
                }
            }
            if (connectMethod != null) {
                try {
                    connectMethod.invoke(mWifiManager, netId);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        } else {
            // < android 4.0
            return null;
        }
        return connectMethod;
    }

    /**
     * Method to Get Signal Strength
     *
     * @return int contain Signal Strength
     */
    public int getSignalStrength(){
        if(mWifiManager!=null){
            return WifiManager.calculateSignalLevel(mWifiInfo.getRssi(), 100);
        }
        return 0;
    }

    /**
     * Method to Ping  IP Address
     *
     * @param addr IP address you want to ping it
     * @return true if the IP address is reachable
     */
    public boolean pingCmd(String addr){
        try {
            String ping = "ping  -c 1 -W 3 " + addr;
            Runtime run = Runtime.getRuntime();
            Process pro = run.exec(ping);
            try {
                pro.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int exit = pro.exitValue();
            //ip address is not reachable
            return exit == 0;
        }
        catch (IOException e) {
        }
        return false;
    }

    public int getDhcpInfo() {
        DhcpInfo dhcp = mWifiManager.getDhcpInfo();
        return dhcp.gateway;
    }

    public boolean setHotSpot(String SSID, String passWord, boolean open){
    	/*
    	 * Before setting the HotSpot with specific Id delete the default AP Name.
    	 */
    	/*
    		List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
    	 for( WifiConfiguration i : list ) {
    	  if(i.SSID != null && i.SSID.equals(SSID)) {
    	     //wm.disconnect();
    	     //wm.enableNetwork(i.networkId, true);
    	     //wm.reconnect();
    		  //mWifiManager.disableNetwork(i.networkId);
    		  mWifiManager.removeNetwork(i.networkId);
    		  mWifiManager.saveConfiguration();
    	     break;
    	  }
    	 }
    	*/
        //mWifiManager.acquire();
        Method[] mMethods = mWifiManager.getClass().getDeclaredMethods();

        for(Method mMethod: mMethods){

            if(mMethod.getName().equals("setWifiApEnabled")) {
                WifiConfiguration netConfig = new WifiConfiguration();
                if(passWord==""){
                    netConfig.SSID = SSID;
                    netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                }else{
                    netConfig.SSID = SSID ;
                    netConfig.preSharedKey = passWord;
                    netConfig.hiddenSSID = false;
                    netConfig.status = WifiConfiguration.Status.ENABLED;
                    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

                }
                try {
                    mMethod.invoke(mWifiManager, netConfig,open);
                    mWifiManager.saveConfiguration();
                    return true;

                } catch (Exception e) {

                }
            }
        }
        return false;
    }

}